package com.softwareinlife.xmpp.client.connect;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import android.app.Activity;
import android.util.Log;

import com.softwareinlife.xmpp.client.GoogleSASLPlainMechanism;

public class XMPPConnector extends Thread {

	/**
	 * XMPP Log Tag
	 */
	private static final String LOG_TAG = "XMPP";

	/**
	 * Activity
	 */
	private static Activity activity = null;

	/**
	 * JID
	 */
	private String jid = null;

	/**
	 * Password
	 */
	private String pwd = null;

	/**
	 * Message Listener
	 */
	private MessageListener messageListener = null;

	/**
	 * Constructor
	 * 
	 * @param context
	 */
	public XMPPConnector(Activity activity, String jid, String pwd) {
		this.activity = activity;
		this.jid = jid;
		this.pwd = pwd;

		Log.d(LOG_TAG, "XMPPConnector was created!");
	}

	/**
	 * Constructor
	 * 
	 * @param messageListener
	 * @param jid
	 * @param pwd
	 */
	public XMPPConnector(MessageListener messageListener, String jid, String pwd) {
		this.messageListener = messageListener;
		this.jid = jid;
		this.pwd = pwd;

		Log.d(LOG_TAG, "XMPPConnector was created!");
	}

	@Override
	public void run() {
		SASLAuthentication.unregisterSASLMechanism("PLAIN");
		SASLAuthentication.registerSASLMechanism("PLAIN",
				GoogleSASLPlainMechanism.class);

		ConnectionConfiguration connConfig = new ConnectionConfiguration(
				"talk.google.com", 5222, "gmail.com");

		XMPPConnection xmpp = new XMPPConnection(connConfig);
		try {
			xmpp.connect();

			// for other jabber accounts, truncate after the @
			// xmpp.login("username", "password");

			// for gtalk / gmail, include the @
			xmpp.login(jid, pwd);
			Log.v(LOG_TAG, "Log in");
		} catch (XMPPException e) {
			Log.v(LOG_TAG, "Failed to connect to " + xmpp.getHost());
			e.printStackTrace();
		}

		try {
			Chat chat = xmpp.getChatManager().createChat(
					"xmppsoftwareinlife@appspot.com", messageListener);
			chat.sendMessage("Howdy1!");
		} catch (XMPPException e) {
			Log.v(LOG_TAG, "Chat exception is " + e);
			e.printStackTrace();
		}

		try {
			Chat chat = xmpp.getChatManager().createChat(
					"xmppsoftwareinlife@appspot.com", messageListener);
			chat.sendMessage("Howdy2!");
		} catch (XMPPException e) {
			Log.v(LOG_TAG, "Chat exception is " + e);
			e.printStackTrace();
		}

		try {
			Chat chat = xmpp.getChatManager().createChat(
					"xmppsoftwareinlife@appspot.com", messageListener);
			chat.sendMessage("Howdy3!");
		} catch (XMPPException e) {
			Log.v(LOG_TAG, "Chat exception is " + e);
			e.printStackTrace();
		}

		// Accept only messages from friend@gmail.com
		PacketFilter filter = new AndFilter(
				new PacketTypeFilter(Message.class), new FromContainsFilter(
						"xmppsoftwareinlife@appspot.com"));

		// Collect these messages
		PacketCollector collector = xmpp.createPacketCollector(filter);

		while (true) {
			Packet packet = collector.nextResult();

			if (packet instanceof Message) {
				Message msg = (Message) packet;

				// Process message
				Log.v(LOG_TAG, "Got message:" + msg.getBody());
			}
		}
	}
}
